<script lang="ts" setup>
import {onMounted, ref} from 'vue';
import request from "../../service/request";
import {message} from 'ant-design-vue';
import {Tool} from "@/util/tool";

const categorys = ref();//所有分类
const tree = ref();//表格树形结构
const category: any = ref({});//当前分类
const selectValue = ref();//下拉框绑定的数据
let selectValueCopy: any;//用来比对两次selectValue是否一致的复制数据
const loading = ref(false);//表格数据加载等待
const visible = ref<boolean>(false);//是否显示对话框
const confirmLoading = ref<boolean>(false);//确认按钮是否显示等待效果
/**
 * 表格绑定的columns
 */
const columns = [
  {
    title: '名称',
    dataIndex: 'name',
    key: 'name',
  },
  {
    title: '父分类',
    dataIndex: 'parent',
    key: 'parent',

  },
  {
    title: '顺序',
    dataIndex: 'sort',
    key: 'sort'
  },
  {
    title: 'Action',
    key: 'action',
  }
];
/**
 * 数据查询
 **/
const handleQuery = () => {
  loading.value = true;
  request.get("/category/all", {}).then((res) => {
    loading.value = false;
    categorys.value = res.data
    tree.value = [];
    tree.value = Tool.array2Tree(categorys.value, 0);
  })
};
/**
 * 编辑分类
 * @param record
 */
const edit = (record: any) => {
  visible.value = true
  category.value = Tool.copy(record)
  selectValue.value = getCategoryName(record.parent)
  selectValueCopy = Tool.copy(selectValue.value)
};
/**
 * 添加分类
 */
const add = () => {
  visible.value = true
  category.value = {}
};
/**
 * 删除分类
 * @param id
 */
const del = (id: number) => {
  request.delete("/category/delete/" + id
  ).then((res) => {
    const data = res.data;
    if (data.success) {
      handleQuery()
    }else {
      message.error(data.message)
    }
  })
};
/**
 * 由id转化名称
 * @param cid
 */
const getCategoryName = (cid: number) => {
  let result = "无";
  categorys.value.forEach((item: any) => {
    if (item.id == cid) {
      result = item.name
    }
  })
  return result;
};
/**
 * 对话框确认事件
 */
const handleOk = () => {
  confirmLoading.value = true;
  if (selectValueCopy == selectValue.value) {
    selectValue.value=category.value.parent
  }
  category.value.parent = selectValue.value
  request.post("/category/save", category.value
  ).then((res) => {
    confirmLoading.value = false;
    const data = res.data;
    if (data.success) {
      visible.value = false;
      handleQuery()
    } else {
      message.error(data.message)
    }
  })
};
/**
 * 初始加载
 */
onMounted(() => {
  handleQuery();
});

</script>

<template>
  <a-layout>
    <a-layout-content :style="{ background: '#fff', padding: '24px', margin: 0, minHeight: '280px' }">
      <p><a-form
          layout="inline"
      >
        <a-form-item
        >
          <a-button type="primary" @click="add()">
            新增
          </a-button>
        </a-form-item>
      </a-form></p>

      <a-table
          :columns="columns"
          :data-source="tree"
          :row-key="record => record.id"
          :loading="loading"
          :pagination="false"
      >
        <template #bodyCell="{column,record }">
          <template v-if="column.key==='cover'">
            <img v-if="record.cover" :src="record.cover" alt="avatar">
          </template>
          <template v-if="column.key==='action'">
            <a-space size="small">
              <a-button type="primary" @click="edit(record)">
                编辑
              </a-button>
              <a-popconfirm
                  title="删除后不可恢复,确认删除吗?"
                  ok-text="确认"
                  cancel-text="取消"
                  @confirm="del(record.id)"
              >
                <a-button type="danger">
                  删除
                </a-button>
              </a-popconfirm>

            </a-space>
          </template>
        </template>

      </a-table>
    </a-layout-content>
  </a-layout>
  <a-modal
      v-model:visible="visible"
      title="分类表单"
      :confirm-loading="confirmLoading"
      @ok="handleOk"
  >
    <a-form :model="category" :label-col="{ span: 3 }" :wrapper-col="{ span: 18 }">
      <a-form-item label="名称">
        <a-input v-model:value="category.name"/>
      </a-form-item>
      <a-form-item label="父分类">
        <a-select
            v-model:value="selectValue"
        >
          <a-select-option value="0">无</a-select-option>
          <a-select-option v-for="t in tree" :key="t.id" :value="t.id" :disabled="category.id===t.id">{{ t.name }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="顺序">
        <a-input v-model:value="category.sort" type="textarea"/>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<style scoped>

</style>